2 using System.Collections.Generic;
5 using System.Threading;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
10 namespace SuperPolarity
15 public static Color BlueColor;
16 public static Color RedColor;
18 ParticleEngine particleEngine;
20 protected bool Shooting;
21 protected int ShotCooldown;
23 public MainShip(Game newGame) : base(newGame) {}
27 particleEngine = null;
30 public override void Initialize(Texture2D texture, Vector2 position)
32 base.Initialize(texture, position);
34 MainShip.BlueColor = new Color(230, 244, 249);
35 MainShip.RedColor = new Color(255, 234, 241);
38 SetPolarity(Polarity.Positive);
45 void InitParticleEngine()
47 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
52 InputController.Bind("moveX", HandleHorizontalMovement);
53 InputController.Bind("moveY", HandleVerticalMovement);
54 InputController.Bind("changePolarity", HandleChangePolarity);
55 InputController.Bind("shoot", HandleShot);
58 protected void HandleShot(float value)
60 Children.Add(ActorFactory.CreateBullet(Position, Angle));
62 Timer t = new Timer(new TimerCallback(UnlockShot));
63 t.Change(ShotCooldown, Timeout.Infinite);
66 protected void UnlockShot(object state)
68 InputController.Unlock("shoot");
71 protected void HandleChangePolarity(float value)
76 public void HandleHorizontalMovement(float value)
78 Acceleration.X = value * AccelerationRate;
80 if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
85 if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
91 public void HandleVerticalMovement(float value)
93 Acceleration.Y = value * AccelerationRate;
96 public override void SwitchPolarity()
98 base.SwitchPolarity();
99 SwitchParticleEngine(CurrentPolarity);
102 public override void SetPolarity(Polarity newPolarity)
104 base.SetPolarity(newPolarity);
105 SwitchParticleEngine(newPolarity);
108 protected void SwitchParticleEngine(Polarity polarity)
110 if (polarity == Polarity.Positive)
112 particleEngine.Color = MainShip.RedColor;
114 else if (polarity == Polarity.Negative)
116 particleEngine.Color = MainShip.BlueColor;
120 particleEngine.Color = Color.Gray;
124 public override void Update(GameTime gameTime)
126 base.Update(gameTime);
127 particleEngine.EmitterLocation = Position;
128 particleEngine.Update();
133 public override void Move(GameTime gameTime)
139 if (Velocity.X > ActVelocity)
141 Velocity.X = ActVelocity;
144 if (Velocity.X < -ActVelocity)
146 Velocity.X = -ActVelocity;
149 if (Velocity.Y > ActVelocity)
151 Velocity.Y = ActVelocity;
154 if (Velocity.Y < -ActVelocity)
156 Velocity.Y = -ActVelocity;
161 public override void Magnetize(Ship ship, float distance, float angle)
165 protected void ConstrainToEdges()
176 if (Position.X > game.GraphicsDevice.Viewport.Width)
178 Position.X = game.GraphicsDevice.Viewport.Width;
194 if (Position.Y > game.GraphicsDevice.Viewport.Height)
196 Position.Y = game.GraphicsDevice.Viewport.Height;
205 public override void Draw(SpriteBatch spriteBatch)
207 particleEngine.Draw(spriteBatch);
208 base.Draw(spriteBatch);